namespace EyeOpen.Web.Mvc
{
	using System.IO;
	using System.Web.Mvc;

	using EyeOpen.Web.Mvc.Models;

	public class BaseController 
		: Controller
	{
		protected string RenderRazorViewToString(string viewName, object model)
		{
			this.ViewData.Model = model;

			using (var sw = new StringWriter())
			{
				var viewResult = ViewEngines.Engines.FindPartialView(this.ControllerContext, viewName);
				var viewContext = new ViewContext(this.ControllerContext, viewResult.View, this.ViewData, this.TempData, sw);
				viewResult.View.Render(viewContext, sw);
				return sw.GetStringBuilder().ToString();
			}
		}

		protected ActionResult ShowMessage(string message)
		{
			return this.Render("Done", new MessageModel(message));
		}

		protected ActionResult Render(string viewName, object model = null)
		{
			if ((Request == null) || Request.IsAjaxRequest())
			{
				return
					model == null ?
						PartialView(viewName) : PartialView(viewName, model);
			}

			return
				model == null ?
				View(viewName) : View(viewName, model);
		}
	}
}